<?php
// ----- >> Extends From Template
class thisPDF extends PDF{
	function Header(){}
	function Footer(){}
}

  function terbilang_get_valid($str,$from,$to,$min=1,$max=9){
      $val=false;
      $from=($from<0)?0:$from;
      for ($i=$from;$i<$to;$i++){
        if (((int) $str{$i}>=$min)&&((int) $str{$i}<=$max)) $val=true;
      }
      return $val;
  } 

  function terbilang_get_str($i,$str,$len){
      $numA=array("","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan");
      $numB=array("","se","dua ","tiga ","empat ","lima ","enam ","tujuh ","delapan ","sembilan ");
      $numC=array("","satu ","dua ","tiga ","empat ","lima ","enam ","tujuh ","delapan ","sembilan ");
      $numD=array(0=>"puluh",1=>"belas",2=>"ratus",4=>"ribu", 7=>"juta", 10=>"milyar", 13=>"triliun");
      $buf="";
      $pos=$len-$i;
      switch($pos){
        case 1:
            if (!terbilang_get_valid($str,$i-1,$i,1,1))
              $buf=$numA[(int) $str{$i}];
          break;
        case 2: case 5: case 8: case 11: case 14:
            if ((int) $str{$i}==1){
              if ((int) $str{$i+1}==0)
                $buf=($numB[(int) $str{$i}]).($numD[0]);
              else
                $buf=($numB[(int) $str{$i+1}]).($numD[1]);
            }
            else if ((int) $str{$i}>1){
                $buf=($numB[(int) $str{$i}]).($numD[0]);
            }       
          break;
        case 3: case 6: case 9: case 12: case 15:
            if ((int) $str{$i}>0){
                $buf=($numB[(int) $str{$i}]).($numD[2]);
            }
          break;
        case 4: case 7: case 10: case 13:
            if (terbilang_get_valid($str,$i-2,$i)){
              if (!terbilang_get_valid($str,$i-1,$i,1,1))
                $buf=$numC[(int) $str{$i}].($numD[$pos]);
              else
                $buf=$numD[$pos];
            }           else if((int) $str{$i}>0){
              if ($pos==4)
                $buf=($numB[(int) $str{$i}]).($numD[$pos]);
              else
                $buf=($numC[(int) $str{$i}]).($numD[$pos]);
            }
          break;
      }
      return $buf;
  }
  function toTerbilang($nominal){
      $buf="";
      $str=$nominal."";
      $len=strlen($str);
      
      for ($i=0;$i<$len;$i++){
        $buf=trim($buf)." ".terbilang_get_str($i,$str,$len);
      }
      return trim($buf);
  }


    $tgl_old = str_replace("/", "", $data['tanggal_invoice']);


    $tahun_new = substr($tgl_old, 4, 4);
    $bulan_new = substr($tgl_old, 2, 2);
    $hari_new   = substr($tgl_old, 0, 2);
    if($bulan_new<='09'){
      $y = substr($bulan_new, 1, 1);

      $hasil = "";
      $iromawi =
      array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September");
      if(array_key_exists($y,$iromawi)){
        $hasil = $iromawi[$y];
      }

      $tgl_new = $hari_new." - ".$hasil." - ".$tahun_new." ";

    } else {

      $hasil = "";
      $iromawi =
      array("","","","","","","","","","","Oktober","November","Desember");
      if(array_key_exists($bulan_new,$iromawi)){
        $hasil = $iromawi[$bulan_new];
      }

      $tgl_new = $hari_new." - ".$hasil." - ".$tahun_new." ";

    }  

    $tgl_old1 = str_replace("/", "", $data['due_date']);

    $tahun_new1 = substr($tgl_old1, 4, 4);
    $bulan_new1 = substr($tgl_old1, 2, 2);
    $hari_new1   = substr($tgl_old1, 0, 2);
    if($bulan_new1<='09'){
      $y1 = substr($bulan_new1, 1, 1);

      $hasil1 = "";
      $iromawi1 =
      array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September");
      if(array_key_exists($y1,$iromawi1)){
        $hasil1 = $iromawi1[$y1];
      }

      $tgl_new1 = $hari_new1." - ".$hasil1." - ".$tahun_new1." ";

    } else {

      $hasil1 = "";
      $iromawi1 =
      array("","","","","","","","","","","Oktober","November","Desember");
      if(array_key_exists($bulan_new1,$iromawi1)){
        $hasil1 = $iromawi1[$bulan_new1];
      }

      $tgl_new1 = $hari_new1." - ".$hasil1." - ".$tahun_new1." ";

    }

    $periode_tagihan = str_replace("-", " ", $data['periode_tagihan']);

    
// ----- >> Init
$fpdf = new thisPDF('P','mm','Letter');

// ------ >> add Font : sample antqua

function fpdf_font_info($metric_file) {
  $name = false;
  require($metric_file);
  if($name) {
    return strpos($name, "-") === false ? array($name, 'Regular') : explode("-", $name);
  } else {
    return array(false, false);
  }
}

function available_fonts($fpdf) {
  $names = array();
  $path = $fpdf->_getfontpath();
  $metric_files = glob("{$path}*.php");

  foreach($metric_files as $f) {
    list($name, $style) = fpdf_font_info($f);
    if($name) {
      $names[] = "$name ($style)";
    }
  }

  return $names;
}

function register_fonts($fpdf) {
  $path = $fpdf->_getfontpath();
  $metric_files = glob("{$path}*.php");

  foreach($metric_files as $f) {
    list($base_name, $style_name) = fpdf_font_info($f);
    if($base_name) {
      switch($style_name) {
        case 'Bold':
          $style = 'B';
          break;
        case 'Italic':
          $style = 'I';
          break;
        case 'BoldItalic':
          $style = 'BI';
          break;
        default:
          $style = '';
      }
      $fpdf->AddFont($base_name, $style, basename($f));
    }
  }
}

register_fonts($fpdf);

//------->> End Font

$fpdf->AddPage();
$fpdf->SetLeftMargin(8);

$fpdf->Ln(0);
$ln_header_y_top	= $fpdf->GetY();


// ----- >> Header
$fpdf->Ln(2);
$fpdf->SetFont('calibri','','14');
//$fpdf->SetFont('calibri','',14);
$fpdf->Cell(0,4,"INVOICE",0,0,'L');

$fpdf->Ln(7);



// ----- >> Data Master
$fpdf->SetFont('calibri','',10);
$fpdf->Cell(60,5,$data['no_invoice'],0,0,'L');
$fpdf->Cell(130,5,"Cilegon, ".$tgl_new,0,0,'R');
$fpdf->Line(9, 25, 200-3, 25); // 197mm from each edge
$fpdf->Ln(7);

$fpdf->Cell(60,5,"Nama Pelanggan",0,0,'L');

$fpdf->Ln(5);
$fpdf->Cell(95,5,$data['nama_perusahaan'],0,0,'L');
$fpdf->Cell(50,5,"CUSTCODE",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(10,5,$data['kode_customer'],0,0,'L');

$fpdf->Ln(5);
$fpdf->Cell(95,5,"",0,0,'L');
$fpdf->Cell(50,5,"Periode",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(10,5,$periode_tagihan,0,0,'L'); 
$fpdf->Ln(5);

$fpdf->MultiCell(95,5,$data['alamat'],0,'L');

$fpdf->Cell(95,5,$data['kota']."  ".$data['kode_pos'],0,0,'L');
$fpdf->Cell(50,5,"Contact Person",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(10,5,$data['nama'],0,0,'L');
$fpdf->Ln(5);

$fpdf->Cell(95,5,"",0,0,'L');
$fpdf->Cell(50,5,"No. FPB/Kontrak",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(10,5,$data['kode_kontrak'],0,0,'L');

$fpdf->Ln(5);
$fpdf->Cell(95,5,"",0,0,'L');
$fpdf->Cell(50,5,"Jatuh Tempo/Due Date",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(10,5,$tgl_new1,0,0,'L');
 
$fpdf->Ln(5);
$fpdf->Line(9, 65, 200-3, 65); // 197mm from each edge
$fpdf->Cell(100,5,"Uraian   :",0,0,'L');
$fpdf->Ln(10);

if ($data['input_ppn']==""){
    $ppn = 10;
} else {
    $ppn = $data['input_ppn'];
}


  if($mode_cetak=="normal"){


    //setting prorate-------------------------------------------------------------------------------------------------------------------------------//
    $query1 = $this->db->query("SELECT TO_CHAR(tgl_aktifasi,'dd/mm/yyyy') as tgl_aktifasi FROM isp_client_kontrak WHERE no_id = '".$data['no_id']."'");
      if($query1->row_array()){
        $getquery1 = $query1->row_array();
        $tanggal = $getquery1['tgl_aktifasi'];
      }

      $query = $this->db->query("SELECT LAST_DAY(tgl_aktifasi) as prorate from isp_client_kontrak WHERE kode_kontrak='".$data['kode_kontrak']."' ");
      $result = $query->row_array();

      $tanggal_pembanding = explode("-", $result['prorate']);

      /*$bulan_periode = explode("-", $data['periode_tagihan']);
      $hasil = "";
      $iromawi =
      array("Januari"=>"01","Februari"=>"02","Maret"=>"03","April"=>"04","Mei"=>"05","Juni"=>"06","Juli"=>"07","Agustus"=>"08","September"=>"09","Oktober"=>"10","November"=>"11","Desember"=>"12");
      if(array_key_exists($bulan_periode[0],$iromawi)){
        $hasil = $iromawi[$bulan_periode[0]];
      }*/
      
      $query3 = $this->db->query("SELECT TO_CHAR(ADD_MONTHS(tanggal_invoice,-1),'dd/mm/yyyy') as tanggal_invoice FROM isp_client_invoice WHERE id_invoice = '".$data['id_invoice']."'");
      if($query3->row_array()){
        $getquery3 = $query3->row_array();
        $triger = $getquery3['tanggal_invoice'];
      }

      //echo $triger;

      $triger = explode("/", $triger);  

      $tanggal_aktifasi = explode("/", $tanggal);


    if($tanggal_aktifasi[0]!=1 && $tanggal_aktifasi[1]==$triger[1] && $tanggal_aktifasi[2]==$triger[2]){

            $tanggal_prorate = ($tanggal_pembanding[0] + 1)-$tanggal_aktifasi[0];
        
            $num = $data['abodemen'] ;
            $num_prorate = $num / $tanggal_pembanding[0];
            $harga_prorate = $num_prorate * $tanggal_prorate;
            $formattedNum = number_format($harga_prorate);

            $formattedNum4 = number_format($data['biaya_pendaftaran']);

            $fpdf->Cell(140,5,"Biaya Pendaftaran",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(20,5,$formattedNum4.",-" ,0,0,'R');
            $fpdf->Ln(5);

            //$num1 = $data['ppn'] ;
            $ppn_prorate = ($harga_prorate + $data['biaya_pendaftaran']) * 10 / 100;
            $formattedNum1 = number_format($ppn_prorate);

            $fpdf->Cell(140,5,"Paket berlangganan Internet : ".$data['nama_jenis_barang']." ".$data['bandwidth'],0,0,'L');
            $fpdf->Ln(5);
            $fpdf->Cell(140,5,"(prorate ".$tanggal_prorate." hari)",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(21,5,$formattedNum.",- ",0,0,'R');
            $fpdf->Ln(5);


            $fpdf->Cell(140,5,"PPN  ".$ppn." %",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(24,5,$formattedNum1.",-(+)" ,0,0,'R');
            $fpdf->Line(158, 95, 193, 95); // 197mm from each edge
            $fpdf->Ln(5);

            //$num2 = $data['total'] ;
            $total_prorate = intval($ppn_prorate) + intval($harga_prorate) + $data['biaya_pendaftaran'];
            $formattedNum2 = number_format($total_prorate);

            $fpdf->Cell(140,5,"SUB TOTAL",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(21,5,$formattedNum2.",- ",0,0,'R');
            $fpdf->Ln(15);
            //echo $total_prorate;
            $fpdf->Cell(140,5,"Terbilang  :",0,0,'L');
            $fpdf->Ln(5);
            $fpdf->SetFont("");
            $fpdf->SetFont('calibri','BI',12);
            $fpdf->Cell(50,5,"#".toTerbilang($total_prorate)." Rupiah #",0,0,'L');
            $fpdf->Ln(10);
            $fpdf->SetFont("");

            $fpdf->SetFont('calibri','',10);

          } else {

            $formattedNum4 = number_format($data['biaya_pendaftaran']);

            $fpdf->Cell(140,5,"Biaya Pendaftaran",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(20,5,$formattedNum4.",-" ,0,0,'R');
            $fpdf->Ln(5);

            $num = $data['abodemen'] ;
            $formattedNum = number_format($num);

            $fpdf->Cell(140,5,"Paket berlangganan Internet : ".$data['nama_jenis_barang']." ".$data['bandwidth'],0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(21,5,$formattedNum.",- ",0,0,'R');
            $fpdf->Ln(5);

            $num1 = $data['ppn'] ;
            $formattedNum1 = number_format($num1);

            $fpdf->Cell(140,5,"PPN  ".$ppn." %",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(25,5,$formattedNum1.",-(+)" ,0,0,'R');
            $fpdf->Line(158, 90, 193, 90); // 197mm from each edge
            $fpdf->Ln(5);

            $num2 = $data['total'] ;
            $formattedNum2 = number_format($num2);

            $fpdf->Cell(140,5,"TOTAL",0,0,'L');
            $fpdf->Cell(10,5,":",0,0,'L');
            $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
            $fpdf->Cell(21,5,$formattedNum2.",- ",0,0,'R');
            $fpdf->Ln(15);

            $fpdf->Cell(140,5,"Terbilang  :",0,0,'L');
            $fpdf->Ln(5);
            $fpdf->SetFont("");
            $fpdf->SetFont('calibri','BI',12);
            $fpdf->Cell(50,5,"#".$data['terbilang']." #",0,0,'L');
            $fpdf->Ln(10);
            $fpdf->SetFont("");

            $fpdf->SetFont('calibri','',10);

      }

	} else if ($mode_cetak=="custom") {
      $total_baru = 0;
      foreach ($data_custom as $key => $data_custom_value) {
       $x=80;
          $fpdf->Cell(140,5,$data_custom_value['uraian'],0,0,'L');
          $fpdf->Cell(10,5,":",0,0,'L');
          $fpdf->Cell(17.5,5,"Rp. " ,0,0,'L');
          $fpdf->Cell(14,5,number_format($data_custom_value['harga']).",- ",0,0,'R');
          $fpdf->Ln(5);

          $total_baru = $total_baru + $data_custom_value['harga'];
      }

          
			$num1 = $total_baru * 0.1;
			$formattedNum1 = number_format($num1);

			$fpdf->Cell(140,5,"PPN 10%",0,0,'L');
			$fpdf->Cell(10,5,":",0,0,'L');
      $fpdf->Cell(10,5,"Rp. " ,0,0,'L');
      $fpdf->Cell(25,5,$formattedNum1.",-(+)",0,0,'R');
      
      $fpdf->Ln(5);

			$num2 = $total_baru + $num1;
			$formattedNum2 = number_format(round($num2));

			$fpdf->Cell(140,5,"TOTAL",0,0,'L');
      $fpdf->Cell(10,5,":",0,0,'L');
			$fpdf->Cell(11,5,"Rp. " ,0,0,'L');
      $fpdf->Cell(21,5,$formattedNum2.",- ",0,0,'R');
      $fpdf->Ln(15);

      $fpdf->Cell(140,5,"Terbilang  :",0,0,'L');
      $fpdf->Ln(5);
      $fpdf->SetFont("");
      $fpdf->SetFont('calibri','BI',12);
      $fpdf->Cell(50,5,"#".toTerbilang(round($num2))." Rupiah #",0,0,'L');
      $fpdf->Ln(10);

	}

$fpdf->SetFont('calibri','',10);
$fpdf->Cell(140,5,"Pembayaran Via Rekening",0,0,'L');
$fpdf->Ln(10);

$fpdf->Cell(140,5,"BANK MANDIRI - Cilegon Anyer",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"No. Rek : 163-0000-25346-1",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"a/n : PRIMKOKAS Cyber-K",0,0,'L');
$fpdf->Ln(10);

$fpdf->Cell(140,5,"*Jika mentransfer pembayaran mohon mencantumkan kode pelanggan ini : ".$data['kode_customer']." pada berita acara",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"Hormat Kami, ",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"Cyber-K PRIMKOKAS",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"Information System Division",0,0,'L');
$fpdf->Ln(20);
$fpdf->Image(APPPATH . 'file/media/ttd_isp.jpg');


$fpdf->Ln(5);
$fpdf->Cell(140,5,"Maulana Rusli",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"Information System Div. Manager",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5,"Tembusan :",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5," - Accounting Dept.",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5," - Finance Dept.",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(140,5," - Arsip",0,0,'L');


echo $fpdf->Output('Laporan Invoice '.$data['no_invoice'].'.pdf', 'I');
exit();
?>